﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Contacts.Maui.Models;
using Contacts.Maui.Views_MVVM;
using Contacts.UseCases.Interfaces;

//using Contact = Contacts.Maui.Models.Contact;
using Contact = Contacts.CoreBusiness.Contact;

namespace Contacts.Maui.ViewModels
{
    public partial class ContactViewModel : ObservableObject
    {
        //public Contact Contact { get; set; }

        public bool IsNameProvided { get; set; }
        public bool IsEmailProvided { get; set; }
        public bool IsEmailFormatValid { get; set; }

        private Contacts.CoreBusiness.Contact contact;

        public Contacts.CoreBusiness.Contact Contact
        {
            get => contact;
            set
            {
                SetProperty(ref contact, value);
            }
        }

        private readonly IViewContactUseCase ViewContactUseCase;
        private readonly IEditContactUseCase editContactUseCase;
        private readonly IAddContactUseCase addContactUseCase;

        public ContactViewModel(IViewContactUseCase ViewContactUseCase, IEditContactUseCase EditContactUseCase, IAddContactUseCase AddContactUseCase)
        {
            //this.Contact = ContactRepository.GetContactsById(1);
            this.Contact = new Contacts.CoreBusiness.Contact();
            this.ViewContactUseCase = ViewContactUseCase;
            this.editContactUseCase = EditContactUseCase;
            this.addContactUseCase = AddContactUseCase;
        }

        public async Task LoadContact(int ContactId)
        {
            //this.Contact = ContactRepository.GetContactsById(ContactId);
            this.Contact = await this.ViewContactUseCase.ExecuteAsync(ContactId);
        }

        [RelayCommand]
        public async Task SaveContact()
        {
            //ContactRepository.UpdateContacts(this.Contact.ContactId, this.Contact);
            await this.editContactUseCase.ExecuteAsync(this.Contact.ContactId, this.Contact);
        }

        [RelayCommand]
        public async Task EditContact()
        {
            if (await ValidateContact())
            {
                await editContactUseCase.ExecuteAsync(this.contact.ContactId, this.contact);
                await Shell.Current.GoToAsync($"{nameof(Contacts_MVVM_Page)}");
            }
        }

        [RelayCommand]
        public async Task BackToContacts()
        {
            await Shell.Current.GoToAsync($"{nameof(Contacts_MVVM_Page)}");
        }

        [RelayCommand]
        public async Task AddContact()
        {
            if (await ValidateContact())
            {
                await addContactUseCase.ExecuteAsync(this.contact);
                await Shell.Current.GoToAsync($"{nameof(Contacts_MVVM_Page)}");
            }
        }

        private async Task<bool> ValidateContact()
        {
            if (!this.IsNameProvided)
            {
                await Application.Current.MainPage.DisplayAlert("Error", "Name is required!", "OK");
                return false;
            }

            if (!this.IsEmailProvided)
            {
                await Application.Current.MainPage.DisplayAlert("Error", "Email is required!", "OK");
                return false;
            }

            if (!this.IsEmailFormatValid)
            {
                await Application.Current.MainPage.DisplayAlert("Error", "Email format incorrect!", "OK");
                return false;
            }

            return true;
        }
    }
}
